/************************************************************
       (@_       
  _     ) \_______________________________________________
 (_)@8@8{}<_______________________________________________>
        )_/        
       (@
Liuxp
Website:        http://me.alipay.com/liuxp
Version：       1.0
File name：     PublishService.cs
Author：        刘小平
Date：          ‎2014年‎09‎月23‎日，‏‎14:57:16
Description：   项目文件版本信息
History：       暂无
***********************************************************/
using System;
using System.Data;
using System.Threading;

namespace Publish.Services
{
    /// <summary>
    /// 发布历史信息
    /// </summary>
    public class PublishService : Service
    {
        #region Fields

        //发布历史记录 Top 20
        private const String GetTopSql = "SELECT TOP 20 [Id],[FolderName],[Type],[Files],[CreatorIPv4],[CreatorHostName],[CreatedTime] FROM dbo.Publish ORDER BY [CreatedTime] DESC";

        //当前项目发布总次数
        private const String GetPublishCountSql = "SELECT COUNT(0) FROM Publish WHERE ProjectName = '{0}'";

        #endregion

        #region Delegate Event

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void OnCompleteHander(Object sender, EventArgs e);

        /// <summary>
        /// 获取项目发布次数
        /// </summary>
        public static event OnCompleteHander OnGetCountComplete;

        #endregion

        #region Public Method

        #region Thread Method

        /// <summary>
        /// 读取当前项目发布次数
        /// </summary>
        public static void BindPublishCountThreadStart()
        {
            var bindPublishCountThread = new Thread(x => GetPublishCount(Publish.ProjectName)) { IsBackground = true };
            bindPublishCountThread.Start();//读取当前项目发布次数
        }

        #endregion
        
        /// <summary>
        /// 获取发布历史信息
        /// </summary>
        /// <returns></returns>
        public static DataTable GetHistory()
        {
            DataTable dataTable = null;
            try
            {
                dataTable = sqlHelper.GetDataSet(GetTopSql);
            }
            catch (Exception ex)
            {
                //这句是处理系统从休眠中恢复后已经和数据除失去联系，需要两次请求才能完成。
                dataTable = sqlHelper.GetDataSet(GetTopSql);
            }
            return dataTable;
        }

        #endregion

        #region Private Method

        /// <summary>
        /// 获取发布次数
        /// </summary>
        /// <returns></returns>
        private static void GetPublishCount(string projectName)
        {
            DataTable dataTable = null;
            try
            {
                dataTable = sqlHelper.GetDataSet(string.Format(GetPublishCountSql, projectName));
            }
            catch (Exception ex)
            {
                //这句是处理系统从休眠中恢复后已经和数据除失去联系，需要两次请求才能完成。
                dataTable = sqlHelper.GetDataSet(string.Format(GetPublishCountSql, projectName));
            }

            OnGetCountComplete(dataTable.Rows[0][0], new EventArgs());
        }

        #endregion
    }
}
